package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.dto.excution.PRoleExcution;
import com.fortune.dto.in.pRole.PRoleAddDto;
import com.fortune.dto.in.pRole.PRoleEditDto;
import com.fortune.dto.in.pRole.PRoleListDto;
import com.fortune.dto.in.pRole.PRolePermissionDto;
import com.fortune.entity.PPosition;
import com.fortune.enums.PRoleEnum;
import com.fortune.service.PRoleService;
import com.fortune.vo.pRole.PRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 
 * @author ran
 *
 */
@Api(value = "pRole", description = "平台岗位相关", tags = {"平台岗位相关接口"})
@RestController
@RequestMapping("pRole")
public class PRoleController {
	@Autowired
	private PRoleService pRoleService;
	
	@ApiOperation(value = "岗位列表", nickname = "list", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
		@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
		@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
		@ApiImplicitParam(name="name", value="岗位名称（模糊查询）",paramType="query",dataType="string"),
		@ApiImplicitParam(name="type", value="排序",paramType="query",dataType="int")
	})
    @GetMapping(value = "list",produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<PRole>> list(@Validated @ApiIgnore PRoleListDto listDto) {
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<PRole> data = pRoleService.listMRoles(listDto);
		return new PRoleExcution<PagingData<PRole>>(data,PRoleEnum.SUCCESS);
	}

	@ApiOperation(value = "岗位权限",nickname ="addPermissions",notes = "",tags = {"@石匡"})
	@PostMapping(value = "addPermissions",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> addPermissions(@Validated @RequestBody PRolePermissionDto pRolePermissionDto){
		int num = pRoleService.insertPermissions(pRolePermissionDto);
		if (num < 1){
			return new PRoleExcution<Void>(null,PRoleEnum.FAILED);
		}
		return new PRoleExcution<Void>(null,PRoleEnum.ADD_SUCCESS);
	}

	@ApiOperation(value = "新增岗位",nickname ="add",notes = "",tags = {"@石匡"})
	@PostMapping(value = "add",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(@Validated @RequestBody PRoleAddDto pRoleAddDto){
		PPosition pPosition = pRoleService.getPositionByName(pRoleAddDto.getName());
		if (pPosition!=null) {
			return new PRoleExcution<Void>(null,PRoleEnum.ROLE_NAME_EXSIST);
		}
		pRoleService.insertRole(pRoleAddDto);
		return new PRoleExcution<Void>(null,PRoleEnum.ADD_SUCCESS);
	}

	@ApiOperation(value = "编辑岗位",nickname ="edit",notes = "",tags = {"@石匡"})
	@PutMapping(value = "edit",
	        produces = { MediaType.APPLICATION_JSON_VALUE },
	        consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(@Validated @RequestBody PRoleEditDto editDto){
		PPosition pPosition = pRoleService.getPositionByRoleId(editDto.getRoleId());
		if (pPosition==null) {
			return new PRoleExcution<Void>(null,PRoleEnum.EDIT_ROLE_NOT_EXSIST);
		}
		pPosition = pRoleService.getPositionByNameAndRoleId(editDto.getName(),editDto.getRoleId());
		if (pPosition!=null) {
			return new PRoleExcution<Void>(null,PRoleEnum.ROLE_NAME_EXSIST);
		}
		pRoleService.updateRole(editDto);
		return new PRoleExcution<Void>(null,PRoleEnum.EDIT_SUCCESS);
	}

	@ApiOperation(value = "删除岗位",nickname ="delete",notes = "",tags = {"@石匡"})
	@ApiImplicitParam(name="roleId", value="岗位id",paramType="query",dataType="string", required=true)
	@DeleteMapping(value = "delete",
	        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(@Validated @NotBlank(message="岗位id不能为空") String roleId){
		PPosition pPosition = pRoleService.getPositionByRoleId(roleId);
		if (pPosition==null) {
			return new PRoleExcution<Void>(null,PRoleEnum.DELETE_ROLE_NOT_EXSIST);
		}
		int result = pRoleService.queryUserByRoleId(roleId);
		if (result > 0){
			return new PRoleExcution<Void>(null,PRoleEnum.PEOPLE_USE_FAILED);
		}
		result = pRoleService.deleteRole(roleId);
		if (result==0) {
			return new PRoleExcution<Void>(null,PRoleEnum.DELETE_ROLE_FAILED);
		}
		return new PRoleExcution<Void>(null,PRoleEnum.DELETE_SUCCESS);
	}
}
